//
//  SAPHotTableViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/4.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPHotTableViewCell.h"
#import "Theme-PrefixHeader.pch"
@implementation SAPHotTableViewCell
- (void)dealloc {
    [_carImage release];
    [_carName release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
  
        [SAPNetWorkTool getWithUrl:hotpinpai parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
            NSDictionary *dic = [result valueForKey:@"result"];
            self.twoModel = [[SAPFindCarTwoModel alloc] initWithDataSource:dic];
            [self imageLabel];
        } fail:^(NSError *error) {
            NSLog(@"%@", error);
        }];
    }return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    for (int i = 0; i < self.twoModel.listArray.count; i++) {
        
        if (i < 5) {
            UIImageView *carsPic = (UIImageView *)[self viewWithTag:i + 200];
            UILabel *carsName = (UILabel *)[self viewWithTag:i + 300];
            carsPic.frame = CGRectMake(SCREEN_SIZE.width / 5 * i + 15, 10, 44, 44);
            carsName.frame = CGRectMake(SCREEN_SIZE.width / 5 * i + 15, 44, 44, 44);
        }else {
        UIImageView *carsPic2 = (UIImageView *)[self viewWithTag:i + 200];
        UILabel *carsName2 = (UILabel *)[self viewWithTag:i + 500];
            carsPic2.frame = CGRectMake(SCREEN_SIZE.width / 5 * (i - 5) + 15, 84, 44, 44);
            carsName2.frame = CGRectMake(SCREEN_SIZE.width / 5 * (i - 5) + 15, 118, 44, 44);
        }
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else {
        self.backgroundColor = [UIColor whiteColor];
    }

    
}
- (void)imageLabel {
    if (self.twoModel) {
    for (int i = 0; i < 5; i++) {
        SAPFindCarThreeModel *threeModel = self.twoModel.listArray[i];
        UIImageView *carsPic = [[UIImageView alloc] init];
        UILabel *carsName = [[UILabel alloc] init];
        carsPic.tag = i + 200;
        carsName.tag = i + 300;
        [carsPic sd_setImageWithURL:[NSURL URLWithString:threeModel.img]];
        carsPic.userInteractionEnabled = YES;
        carsName.text = threeModel.name;
        carsName.textAlignment = NSTextAlignmentCenter;
        carsName.font = [UIFont systemFontOfSize:13];
       
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *passWord = [user objectForKey:@"nightModel"];
        
        if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
            carsName.textColor = RGB(153.0,153.0,153.0,1.0f);
            
        }
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [carsPic addGestureRecognizer:tap];
        [self addSubview:carsPic];
        [self addSubview:carsName];
        }
    }for (int i = 5; i < 10; i++) {
        SAPFindCarThreeModel *threeModel = self.twoModel.listArray[i];
        UIImageView *carsPic = [[UIImageView alloc] init];
        UILabel *carsName = [[UILabel alloc] init];
        carsPic.tag = i + 200;
        carsName.tag = i + 500;
        [carsPic sd_setImageWithURL:[NSURL URLWithString:threeModel.img]];
        carsPic.userInteractionEnabled = YES;
        carsName.text = threeModel.name;
        carsName.textAlignment = NSTextAlignmentCenter;
        carsName.font = [UIFont systemFontOfSize:13];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [carsPic addGestureRecognizer:tap];
        [self addSubview:carsPic];
        [self addSubview:carsName];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *passWord = [user objectForKey:@"nightModel"];
        
        if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
            carsName.textColor = RGB(153.0,153.0,153.0,1.0f);
            
        }


    }
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
    _count = tap.view.tag - 200;
    SAPFindCarThreeModel *three = self.twoModel.listArray[_count];
    NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.5/cars/seriesprice-pm1-b%@-t1.json", three.id];
    [self.delegate passHotCarValue:str];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
